# 字体管理
# 创建人：曾逸夫
# 创建时间：2022-01-27


import os
import sys
from urllib import request, error
from pathlib import Path


ROOT_PATH = sys.path[0]  # 项目根目录

fonts_list = ["SimSun.ttc", "TimesNewRoman.ttf"]  # 字体列表
fonts_suffix = ["ttc", "ttf", "otf"]  # 字体后缀
data_url_list = [
    "https://gitee.com/CV_Lab/opencv_webcam/attach_files/959173/download/SimSun.ttc",
    "https://gitee.com/CV_Lab/opencv_webcam/attach_files/959172/download/TimesNewRoman.ttf",
]


# urllib下载进度
def _dowmload(batch_num, batch_size, total_size):

    sys.stdout.write(
        f"\r正在下载字体：{font_name} 下载进度：{int(((batch_num * batch_size / total_size) * 100))}%"
    )
    sys.stdout.flush()


# 创建字体库
def add_fronts():

    global font_name

    for i in range(len(data_url_list)):
        font_name = data_url_list[i].split("/")[-1]  # 字体名称
        Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True)  # 创建目录

        file_path = f"{ROOT_PATH}/fonts/{font_name}"  # 字体路径

        try:
            # 下载字体文件
            request.urlretrieve(data_url_list[i], file_path, _dowmload)
            print()
            print(f'{font_name} {info_warning("字体文件下载完成！")[1]} 已保存至：{file_path}')
        except error.HTTPError as e:
            print(e)


# 判断字体文件
def is_fonts(fonts_dir):
    if os.path.isdir(fonts_dir):
        # 如果字体库存在
        fonts_flag = 0  # 判别标志
        f_list = os.listdir(fonts_dir)  # 本地字体库

        for i in fonts_list:
            if i not in f_list:
                fonts_flag = 1  # 字体不存在

        if fonts_flag == 1:
            # 字体不存在
            print(f'{info_warning("字体不存在，正在加载。。。")[2]}')
            add_fronts()  # 创建字体库
        else:
            print(f'{fonts_list}{info_warning("字体已存在！")[1]}')
    else:
        # 字体库不存在，创建字体库
        print(f'{info_warning("字体库不存在，正在创建。。。")[2]}')
        add_fronts()  # 创建字体库


# 字体颜色（Bash Shell版）
def fonts_color():
    # 字体颜色
    pre_colorFlag = {
        "BLUE": "\033[94m",
        "GREEN": "\033[92m",
        "RED": "\033[91m",
        "YELLOW": "\033[93m",
        "BOLD": "\033[1m",
        "PURPLE": "\033[95m",
        "CYAN": "\033[96m",
        "DARKCYAN": "\033[36m",
        "UNDERLINE": "\033[4m",
    }

    end_colorFlag = "\033[0m"  # 结尾标志符

    return pre_colorFlag, end_colorFlag


# info warning字体颜色设置
def info_warning(msg):
    pre_colorFlag, end_colorFlag = fonts_color()
    info, success, warning = (
        pre_colorFlag["BLUE"] + pre_colorFlag["BOLD"],
        pre_colorFlag["GREEN"] + pre_colorFlag["BOLD"],
        pre_colorFlag["RED"] + pre_colorFlag["BOLD"],
    )

    info_msg = f"{info}{msg}{end_colorFlag}"
    success_msg = f"{success}{msg}{end_colorFlag}"
    warning_msg = f"{warning}{msg}{end_colorFlag}"

    return [info_msg, success_msg, warning_msg]
